import type {
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage,
  ConsultOrderPreData,
  ConsultOrderPreParams,
  DoctorPage,
  FollowType,
  Image,
  KnowledgePage,
  KnowledgeParams,
  PageParams,
  PartialConsult,
  TopDep,
} from '@/types/consult'
import { request } from '@/utils/request'

// 获取知识文章列表
export const getKnowledgeList = (params: KnowledgeParams) => {
  return request<KnowledgePage>('patient/home/knowledge', 'get', params)
}

// 获取医生信息
export const getDoctorList = (params: PageParams) => {
  return request<DoctorPage>('/home/page/doc', 'get', params)
}

// 关注/取消关注 知识点【参数类型定义】：这种简单的请求参数可以直接在函数参数中定义，不用单独写类型
export const followOrUnfollow = (id: string, type: FollowType) =>
  request('/like', 'POST', { id, type })

// 获取所有科室（父子结构）
export const getAllDep = () => request<TopDep[]>('dep/all', 'get')

// 上传图片
// 知识点：这里的File类型是Ts内置的类型，和HTML的文件对象是一样的，不用引入就可以使用（todo 整理一些常见的Ts内置类型？）
export const uploadImg = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request<Image>('/upload', 'post', formData)
}

// 拉取预支付订单信息
export const getConsultOrderPre = (params: ConsultOrderPreParams) =>
  request<ConsultOrderPreData>('/patient/consult/order/pre', 'GET', params)

// 生成订单信息
export const createConsultOrder = (consultData: PartialConsult) =>
  request<{ id: string }>('/patient/consult/order', 'POST', consultData)

// 获取支付地址  0 是微信  1 支付宝
export const getConsultOrderPayUrl = (params: {
  paymentMethod: 0 | 1
  orderId: string
  payCallback: string
}) => {
  return request<{ payUrl: string }>('/patient/consult/pay', 'POST', params)
}

// 获取订单信息
export const getConsultOrderDetail = (orderId: string) =>
  request<ConsultOrderItem>('/patient/consult/order/detail', 'GET', { orderId })

// 查看处方
export const getPrescriptionPic = (id: string) =>
  request<{ url: string }>(`/patient/consult/prescription/${id}`)

// 订单列表查询
export const getConsultOrderList = (params: ConsultOrderListParams) =>
  request<ConsultOrderPage>('/patient/consult/order/list', 'GET', params)

// 取消订单
export const cancelOrder = (id: string) => request(`/patient/order/cancel/${id}`, 'PUT')

// 删除订单
export const deleteOrder = (id: string) => request(`/patient/order/${id}`, 'DELETE')
